import Vue from 'vue'
import Vuex from 'vuex'
import {
  updateHotelCitys,
  initHotelCity,
  initializationCity,
  buildCityindexs
} from './hotelDataTransForm'

Vue.use(Vuex)

const store = new Vuex.Store({
  state: {
    // 是否加密开关
    isCrypto: false,
    // uniqueId: '',
    // RSA_Pubkey: '',
    // timeDiff: ''
    uuidList: [],
    conferenceEquip: '', //会议室设别
    meenum: '', //会议室人数
    startTime: '', //会议室开始时间
    endTime: '', //会议室结束时间
    meetDesc: '', //会议室描述（名字+地点）
    meetSelDate: '', //会议室选择日期
    meetSelBook: '', //会议室查看的预定列表
    hotelCitys: { citys: [], hasData: false },//酒店城市列表缓存
    obtainTrainCitys: { citys: {}, hasData: false },//飞机、火车订单
    obtainPlaneCitys: { citys: {}, hasData: false }//飞机、火车订单
  },
  mutations: {
    // SET_UNIQUEID: (state, uniqueId) => {
    //     state.uniqueId = uniqueId
    // },
    // SET_RSA_PUBKEY: (state, RSA_Pubkey) => {
    //     state.RSA_Pubkey = RSA_Pubkey
    // },
    // SET_TIMEDIFF: (state, timeDiff) => {
    // 	state.timeDiff = timeDiff
    // },
    SET_HOTEL_CITYS: (state, citys) => {
      state.hotelCitys = citys
    },
    SET_OBTAIN_TRAIN_CITYS: (state, citys) => {
      state.obtainTrainCitys = citys
    },
    SET_OBTAIN_PLANE__CITYS: (state, citys) => {
      state.obtainPlaneCitys = citys
    },
    EDIT_EQUIP: (state, item) => {
      state.conferenceEquip = item
    },
    EDIT_MEENUM: (state, item) => {
      state.meenum = item
    },
    EDIT_STATETIME: (state, item) => {
      state.startTime = item
    },
    EDIT_ENDTIME: (state, item) => {
      state.endTime = item
    },
    EDIT_MEETDESC: (state, item) => {
      state.meetDesc = item
    },
    EDIT_MEETSELDATE: (state, item) => {
      state.meetSelDate = item
    },
    EDIT_SELMEETBOOK: (state, item) => {
      state.meetSelBook = item
    },
    ADD_UUID: (state, item) => {
      state.uuidList.push(item)
    },
    DEL_UUID: (state, id) => {
      state.uuidList.forEach((u, i) => {
        if (u.id == id) {
          state.uuidList.splice(i, 1)
        }
      })
    },
  },
  getters: {
    getKey: (state) => (id) => {
      return state.uuidList.find(item => item.id == id)
    },
    getObtainTrainCitys: (state) => () => {
      if (!state.obtainTrainCitys || !state.obtainTrainCitys.hasData) {
        return undefined
      }
      return state.obtainTrainCitys
    },
    getObtainPlaneCitys: (state) => () => {
      if (!state.obtainPlaneCitys || !state.obtainPlaneCitys.hasData) {
        return undefined
      }
      return state.obtainPlaneCitys
    },
    getHotelCitys: (state) => () => {
      if (!state.hotelCitys || !state.hotelCitys.hasData) {
        return undefined
      }
      return state.hotelCitys
    }
  },
  actions: {
    add_uuid ({ commit }, item) {
      commit('ADD_UUID', item)
    },
    del_uuid ({ commit }, id) {
      commit('DEL_UUID', id)
    },
    setHotelCitys ({ commit }, citys) {
      let handleCity = []
      let cityindexs = []
      initHotelCity(handleCity, cityindexs, citys)
      let cityData = updateHotelCitys(citys)
      let hotCityOBJ = {
        citys: citys,
        handleCity: handleCity,
        cityindexs: cityindexs,
        cityData: cityData,
        hasData: true
      }
      commit('SET_HOTEL_CITYS', hotCityOBJ)
    },
    setObtainTrainCitys ({ commit }, citys) {
      let handleCity = initializationCity()
      let cityindexs = []
      let cityData = citys
      buildCityindexs(cityindexs, cityData, 'name', handleCity)
      commit('SET_OBTAIN_TRAIN_CITYS', {
        citys: citys,
        handleCity: handleCity,
        cityindexs: cityindexs,
        cityData: cityData,
        hasData: true
      })
    },
    setObtainPlaneCitys ({ commit }, citys) {
      let handleCity = initializationCity()
      let cityindexs = []
      let cityData = citys
      buildCityindexs(cityindexs, cityData, 'name', handleCity)
      commit('SET_OBTAIN_PLANE__CITYS', {
        citys: citys,
        handleCity: handleCity,
        cityindexs: cityindexs,
        cityData: cityData,
        hasData: true
      })
    },
  }
})

export default store
